package com.moody.init;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;

import com.moody.hibernate.domain.ReportModel;
import com.moody.service.impl.CustomerServiceImpl;
import com.moody.util.EncryptUtil;
import com.moody.util.SystemUtil;

/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-9-2 下午02:32:54
 * @Modified by: Wangfuqiang 2011-09-07
 * @Description: 
 */

//public class InitSpringServletContext implements ServletContextListener  {
public class InitSpringServletContext extends ContextLoaderListener   {
	private static final Logger log = LoggerFactory.getLogger(CustomerServiceImpl.class);
	
	// wfq
	//private final static String  MAC_ADDRESS= "00-21-6A-47-69-22";
	//private final static String  MAC_ADDRESS= "5C-F3-FC-DB-41-DE";//123.0.8.8
	
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		super.contextInitialized(servletContextEvent);
		
		ServletContext servletContext = servletContextEvent.getServletContext();	

//
//		String realKey = "";
//		String key = EncryptUtil.getKeyByAuthorName("LIUPENG");
//		String os = SystemUtil.getOSName();
//        if(os.startsWith("windows")){  
//            //本地是windows  
//            realKey = SystemUtil.getWindowsKey();
//        }else{  
//            //本地是非windows系统 一般就是unix  
//            realKey = SystemUtil.getUnixKey(); 
//        }  
//		if(!key.equals(realKey)) {
//			System.exit(0);
//			return ;
//		}

		DataDictionary.getInstance();
		log.info("加载数据字典");
		servletContext.setAttribute("DataDictionary", DataDictionary.getInstance().getDataDictionary());
		servletContext.setAttribute("City", Region.getInstance().getCity());
		servletContext.setAttribute("CityJSON", Region.getInstance().getCityJSON());
		servletContext.setAttribute("Country", Country.getInstance().getCountry());
		servletContext.setAttribute("CountryJSON", Country.getInstance().getCountryJSON());
		servletContext.setAttribute("Industry", Industry.getInstance().getIndustry());
		servletContext.setAttribute("ReportItem", ReportItemInit.getInstance().getReportItem());
		servletContext.setAttribute("AssetList", ReportModelInit.getInstance().getEnterAssetFromModel());
		servletContext.setAttribute("DebtList", ReportModelInit.getInstance().getEnterDebtFromModel());
		//ReportModelInit.getInstance().insertEnterBalanceSheet();
		//servletContext.setAttribute("EnterIncomeList", ReportModelInit.getInstance().getEnterIncomeList());
		//ReportModelInit.getInstance().insertEnterIncome();
		servletContext.setAttribute("EnterIncomeList", ReportModelInit.getInstance().getEnterIncomeFromModel());
		servletContext.setAttribute("EnterCashFlowList", ReportModelInit.getInstance().getEnterCashFlowFromModel());
		//ReportModelInit.getInstance().insertEnterCashFlow();
		servletContext.setAttribute("EnterDisplayFlagMap", ReportModelInit.getInstance().getDisplayFlagMap());
		servletContext.setAttribute("EnterFinancialIndexList", ReportModelInit.getInstance().getEnterFinanceIndexList());
		//ReportModelInit.getInstance().insertEnterFinancialIndex();
		servletContext.setAttribute("EnterCashFlowGenerationList", ReportModelInit.getInstance().getEnterCashFlowGenerationList());
		//ReportModelInit.getInstance().insertEnterCashFlowGeneration();
		//ReportModelInit.getInstance().insertPublicBalanceSheet();
		servletContext.setAttribute("EnterBalanceSheetList", ReportModelInit.getInstance().getPublicBalanceSheetList());
		servletContext.setAttribute("PublicAssetList", ReportModelInit.getInstance().getPublicAssetFromModel());
		servletContext.setAttribute("PublicDebtList", ReportModelInit.getInstance().getPublicDebtFromModel());		
		//ReportModelInit.getInstance().insertPublicIncome();
		servletContext.setAttribute("PublicRevenueList", ReportModelInit.getInstance().getPublicRevenueFromModel());
		servletContext.setAttribute("PublicExpendList", ReportModelInit.getInstance().getPublicExpendFromModel());
		servletContext.setAttribute("PublicSurplusList", ReportModelInit.getInstance().getPublicSurplusFromModel());	
//		ReportModelInit.getInstance().insertFinanBalanceSheet();
		servletContext.setAttribute("PublicFinancialIndexList", ReportModelInit.getInstance().getPublicFinanceIndexList());
		//ReportModelInit.getInstance().insertPublicFinancialIndex();
		servletContext.setAttribute("FinanAssetList", ReportModelInit.getInstance().getFinanAssetFromModel());
		servletContext.setAttribute("FinanDebtList", ReportModelInit.getInstance().getFinanDebtFromModel());
//		ReportModelInit.getInstance().insertFinanIncomeSheet();
		servletContext.setAttribute("FinanIncomeList", ReportModelInit.getInstance().getFinanIncomeFromModel());
		
	//	ReportModelInit.getInstance().insertFinanFinancialIndexSheet();
		servletContext.setAttribute("FinanFinancialIndexList", ReportModelInit.getInstance().getFinanFinancialIndexFromModel());
		servletContext.setAttribute("FinanFinancialIndexInputList", ReportModelInit.getInstance().getFinanFinancialIndexInputFromModel());

		//ReportModelInit.getInstance().insertSmallBalanceSheet();
		servletContext.setAttribute("SmallBalanceSheetList", ReportModelInit.getInstance().getSmallBalanceSheetList());
		servletContext.setAttribute("SmallAssetList", ReportModelInit.getInstance().getSmallAssetFromModel());
		servletContext.setAttribute("SmallDebtList", ReportModelInit.getInstance().getSmallDebtFromModel());
		ReportModelInit.getInstance().insertSmallCashFlowSheet();
		servletContext.setAttribute("SmallCashFlowList", ReportModelInit.getInstance().getSmallCashFlowFromModel());
		
		ReportModelInit.getInstance().insertSmallIncome();
		servletContext.setAttribute("SmallIncomeList", ReportModelInit.getInstance().getSmallIncomeFromModel());
		
		ReportModelInit.getInstance().insertSmallFinancialIndex();
		servletContext.setAttribute("SmallFinancialIndexList", ReportModelInit.getInstance().getSmallFinancialIndexFromModel());
		
//		ModelInfoInit.importModelInfoFromExcel_Finan();
//		ModelInfoInit.importModelInfoFromExcel_NonFinan();
		
		//加载参数管理信息
		servletContext.setAttribute("LeverParams",ParamModelInit.getInstance().getLeverParams());
		servletContext.setAttribute("IndustryParams",ParamModelInit.getInstance().getIndustryParams());
		servletContext.setAttribute("AreaParams",ParamModelInit.getInstance().getAreaParams());
		servletContext.setAttribute("SolvencyParams",ParamModelInit.getInstance().getSolvencyParams());
		servletContext.setAttribute("CCFParams",ParamModelInit.getInstance().getCCFParams());
		servletContext.setAttribute("IndAccessLine",ParamModelInit.getInstance().getAccessLine());
		servletContext.setAttribute("CustLimitParams",ParamModelInit.getInstance().getLimitParams());
		servletContext.setAttribute("FTPParams",ParamModelInit.getInstance().getFTPParams());
		servletContext.setAttribute("DiscountParams",ParamModelInit.getInstance().getDiscountParams());
		servletContext.setAttribute("OrgMap",OrgInit.getInstance().getOrgInfoMap());
		servletContext.setAttribute("RoleURL",RoleRightInit.getInstance().getRightMap());
		
		log.info("加载完毕");
	}
	
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		log.error("destroy initSpringServlet Context");
	}
}
